import request from "./request/requestToken";
import { RootObject } from "./types/rootObject";

// 获取模板对应变量列表
export enum readWriteMode {
  // 1-读写，2-只读，3-只写
  "读写" = 1,
  "只读" = 2,
  "只写" = 3,
}
export interface VarItem {
  id?: number;
  templateId?: number;
  coefficient: number;
  readWriteMode: readWriteMode;
  varId: number;
  varSort: number;
  dataFormat: string;
  numberFormat: string;
  varJson: string;
  varName: string;
  varType: string;
  varUnit: string;
}
type varListResult = {
  records: VarItem[];
  total: number;
};
type varListParams = { templeId: string; pageNum: number; pageSize: number };
export const apiGetVarList = (data: varListParams) =>
  request<RootObject<varListResult>>({
    method: "POST",
    url: "/acq-equip-variable/page",
    data,
  });

// 获取变量属性（表单第一个右侧select）
export const apiGetVarTypeList = () =>
  request<RootObject<{ date: string[] }>>({
    method: "GET",
    url: "/acq-equip-variable/getTypeList",
  });

// 获取变量名称
export const apiGetVarNameList = (type: string) =>
  request<RootObject<{ date: string[] }>>({
    method: "GET",
    url: "/acq-equip-variable/getNameList",
    params: { type },
  });

// 获取变量单位，变量标识
type unitAndJsonRes = {
  id: number;
  identJson: string;
  name: string;
  type: string;
  unit: string;
};
export const apiGetVarUnitAndJson = (data: { name: string; type: string }) =>
  request<RootObject<unitAndJsonRes>>({
    method: "POST",
    url: "/acq-equip-variable/getVar",
    data,
  });

// 变量详情查询
export interface VarDeailType extends VarItem {
  type: string;
  name: string;
  templateId?: number;
}
export const apiGetVarDetail = (id: number) =>
  request<RootObject<VarDeailType>>({
    method: "GET",
    url: `/acq-equip-variable/detail/${id}`,
  });

// 编辑变量
export const apiUpdateVar = (data: VarItem) =>
  request<RootObject<VarItem>>({
    method: "POST",
    url: "/acq-equip-variable/update",
    data,
  });

// 删除变量
export const apiDellVar = (data: { idList: number[] }) =>
  request<RootObject<VarItem>>({
    method: "POST",
    url: "/acq-equip-variable/delete",
    data,
  });

// 新增变量
export const apiAddVar = (data: VarItem) =>
  request<RootObject<VarItem>>({
    method: "POST",
    url: "/acq-equip-variable/add",
    data,
  });
